/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  ACCTCustomerInfoMapperImpl
 *
 *  Edit History:
 *      lizhangtao Created on 2020年04月09日
 *
 */
package cc.rengu.igas.acct.common.dao.impl;

import cc.rengu.igas.acct.common.constant.AcctSqlMapperId;
import cc.rengu.igas.acct.common.dao.AcctCustomerInfoMapper;
import cc.rengu.igas.acct.common.dao.MapperCommon;
import cc.rengu.igas.acct.common.entity.AcctCustomerInfo;
import cc.rengu.igas.acct.common.enums.AcctRespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;

/**
 * 账户客户信息表操作实现类
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/5/3
 */
public class AcctCustomerInfoMapperImpl extends MapperCommon implements AcctCustomerInfoMapper {
    /**
     * 插入账户客户信息表，数据库事务SQL执行
     * 无提交回滚用于事物begin和end之间
     *
     * @param acctCustomerInfo 账户信息表对应实体
     * @param dbs              数据库操作句柄
     * @return Database.DBS_SUCCESS成功，
     * Database.DBS_PKEY_DUPLICATE主键重复，
     * 违反唯一约束需要自己判断，
     * 其他失败
     */
    @Override
    public int insert2AcctCustomerInfo(AcctCustomerInfo acctCustomerInfo, DbsUtil dbs) {
        try {
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, AcctSqlMapperId.INSERT2CUSTOMER_INFO);
            int iReturnCode = dbs.dbsExecuteTransactionSql(sqlStr, acctCustomerInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("登记账户客户信息失败,iReturnCode:<{}>", iReturnCode);
                return iReturnCode;
            }
            return Database.DBS_SUCCESS;
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            return Integer.MIN_VALUE;
        }
    }

    /**
     * 以法人机构、外部用户类型、客户标识查询账户客户信息
     * 无提交回滚，用于事物begin和end之间
     *
     * @param instId   法人机构
     * @param userType 外部用户类型
     * @param userId   客户标识
     * @param dbs      数据库操作句柄
     * @return 账户客户信息实体
     * @throws BizException 其他数据库异常
     */
    @Override
    public AcctCustomerInfo selectAcctCustomInfoByUserId(String instId, String userType, String userId, DbsUtil dbs) throws BizException {
        try {
            AcctCustomerInfo acctCustomerInfo = new AcctCustomerInfo();
            acctCustomerInfo.setUserType(userType);
            acctCustomerInfo.setUserId(userId);
            acctCustomerInfo.setInstId(instId);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, AcctSqlMapperId.SELECT_CUSTOMER_BY_USER_ID);
            int iReturnCode = dbs.dbsExecuteSql(sqlStr, acctCustomerInfo, acctCustomerInfo);
            if (Database.DBS_SUCCESS == iReturnCode) {
                return acctCustomerInfo;
            } else if (Database.DBS_NOT_FOUND == iReturnCode) {
                rglog.info("未查询到数据,instId<{}> userType<{}> userId<{}>", instId, userType, userId);
                return null;
            } else {
                //查询失败(非未查询到数据)
                throw new BizException(AcctRespCodeEnum.DB_ERROR.getRespCode(),
                        AcctRespCodeEnum.DB_ERROR.getRespDesc());
            }
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            throw new BizException(AcctRespCodeEnum.DB_EXCEPTION.getRespCode(),
                    AcctRespCodeEnum.DB_EXCEPTION.getRespDesc());
        }
    }

    /**
     * 使用法人机构号、客户号查询客户信息
     *
     * @param instId     法人机构号
     * @param customerId 客户号
     * @return 成功返回客户信息、无数据返回null
     * @throws BizException 其他失败及异常抛出异常
     */
    @Override
    public AcctCustomerInfo selectAcctCustomInfoByCustomerId(String instId, String customerId) throws BizException {
        try {
            extractDbPoolName();
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            AcctCustomerInfo acctCustomerInfo = new AcctCustomerInfo();
            acctCustomerInfo.setInstId(instId);
            acctCustomerInfo.setCustomerId(customerId);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE,
                    AcctSqlMapperId.SELECT_CUSTOMER_BY_CUSTOMER_ID);
            int retCode = dbsUtil.dbsExecuteSql(sqlStr, acctCustomerInfo, acctCustomerInfo);
            if (Database.DBS_SUCCESS == retCode) {
                return acctCustomerInfo;
            } else if (Database.DBS_NOT_FOUND == retCode) {
                rglog.info("无数据，instId<{}> customerId<{}>", instId, customerId);
                return null;
            } else {
                throw new BizException(AcctRespCodeEnum.DB_ERROR.getRespCode(),
                        AcctRespCodeEnum.DB_ERROR.getRespDesc());
            }
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            throw new BizException(AcctRespCodeEnum.DB_EXCEPTION.getRespCode(),
                    AcctRespCodeEnum.DB_EXCEPTION.getRespDesc());
        }
    }

    /**
     * 删除客户信息
     * 不带事物，不提交不回滚用于外部事物begin和end之间
     *
     * @param instId     法人机构
     * @param customerId 客户号
     * @param dbs        数据库操作句柄
     * @return Database.DBS_SUCCESS成功，其他失败
     */
    @Override
    public int delCustomerByCustomerId(String instId, String customerId, DbsUtil dbs) {
        try {
            AcctCustomerInfo acctCustomerInfo = new AcctCustomerInfo();
            acctCustomerInfo.setInstId(instId);
            acctCustomerInfo.setCustomerId(customerId);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, AcctSqlMapperId.DEL_CUSTOMER_BY_CUSTOMER_ID);
            return dbs.dbsExecuteTransactionSql(sqlStr, acctCustomerInfo, acctCustomerInfo);
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            return Integer.MIN_VALUE;
        }
    }

    /**
     * 更新客户信息状态
     *
     * @param instId     法人机构号
     * @param customerId 客户号
     * @param status     状态
     * @param dbs        数据库操作句柄
     * @return 成功返回 Database.SUCCESS， 失败返回其他，异常返回Integer.MIN_VALUE
     */
    @Override
    public int updCustomerStatusByCustomerId(String instId, String customerId, String status, DbsUtil dbs) {
        try {
            AcctCustomerInfo acctCustomerInfo = new AcctCustomerInfo();
            acctCustomerInfo.setInstId(instId);
            acctCustomerInfo.setCustomerId(customerId);
            acctCustomerInfo.setUserStatus(status);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, AcctSqlMapperId.UPD_CUSTOMER_STATUS_BY_CUSTOMER_ID);
            return dbs.dbsExecuteTransactionSql(sqlStr, acctCustomerInfo, null);
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            return Integer.MIN_VALUE;
        }
    }


}
